#include "indexpage.h"
#include "ui_indexpage.h"

extern void rmWidget(QVBoxLayout *m_layoutMain);

IndexPage::IndexPage(QWidget *parent) :
    QWidget(parent)
{
    // 初始化数据
    strList << "C" << "C++" << "PHP" << "Java" << "Android" << "HarmonyOS" << "STM32";

    QWidget *widget = new QWidget();
    widget->setMaximumHeight(50);
    // 创建布局
    hLayout = new QHBoxLayout();
    hLayout->setContentsMargins(0, 0, 0, 0);
    vLayout = new QVBoxLayout();
    vLayout->setContentsMargins(12, 3, 12, 3);

    // 设置水平布局
    spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);
    hLayout->addItem(spacer);
    for (int i=0; i<strList.length(); i++)
    {
        QLabel *label = new QLabel(strList.at(i));
        label->setMinimumHeight(50);
        labelList << label;
        label->installEventFilter(this);
        hLayout->addWidget(label);
        hLayout->addItem(spacer);
    }
    // 将水平布局加入到父控件
    widget->setLayout(hLayout);
    // 将父控件加入到垂直布局
    vLayout->addWidget(widget);
    QWidget *innerWidget = new QWidget();
    innerLay = new QVBoxLayout();
    innerWidget->setLayout(innerLay);
    vLayout->addWidget(innerWidget);
//    spacer1 = new QSpacerItem(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);
//    vLayout->addItem(spacer1);
    // 设置当前布局整体为垂直
    setLayout(vLayout);
}
void IndexPage::removeBottom()
{
    rmWidget(innerLay);
}
void IndexPage::addBlogItem(QMap<QString, QString> map)
{
    //vLayout->removeItem(spacer1);
    // 创建item
    BlogItem *item = new BlogItem(this, &map);
    connect(item, SIGNAL(labelClicked(QMap<QString, QString>, int)), this, SLOT(slotBlogInfo(QMap<QString, QString>, int)));
    // 将item加入到垂直布局
    innerLay->addWidget(item);
    // 保存item到list
    itemList<<item;
    item->setMinimumHeight(120);
    item->setMaximumHeight(150);
}

void IndexPage::addResItem(QMap<QString, QString> mapInfo)
{
    QMap<QString, QString> map = mapInfo;
    qint64 fsize = map["file_size"].toInt();
    if(fsize < 1024)
        map["file_size"] = QString::number(fsize)+" B";
    else if(fsize<1024*2024 && fsize>1024)
        map["file_size"] = QString::number(fsize/1024)+" KB";
    else if(fsize>1024*2024 && fsize<1024*1024*1024)
        map["file_size"] = QString::number(fsize/(1024*1024))+" MB";
    else if(fsize>1024*2024*1024 && fsize<1024*1024*1024*1024)
        map["file_size"] = QString::number(fsize/(1024*1024*1024))+" GB";
    //vLayout->removeItem(spacer1);
    // 创建控件
    ShowRes *res = new ShowRes(this, &map);
    // 绑定信号
    connect(res, SIGNAL(signalDownloadRes(QMap<QString, QString>)), this, SLOT(slotDownloadRes(QMap<QString, QString>)));
    connect(res, SIGNAL(signalUserRes(QString)), this, SLOT(slotUserRes(QString)));
    // 将控件加入到布局
    innerLay->addWidget(res);
}

bool IndexPage::eventFilter(QObject *obj, QEvent *event)	// 添加时间过滤器声明
{
    for(int i=0; i<labelList.length(); i++)
    {
        if(obj == labelList.at(i))
        {
            // 鼠标移动到控件上
            if(event->type() == QEvent::Enter)
            {
                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
            }
            // 鼠标离开控件
            else if(event->type() == QEvent::Leave)
            {
                labelList.at(i)->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");
            }
            // 鼠标单击
            else if(event->type() == QEvent::MouseButtonPress)
            {
                QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
                // 左键单击
                if(mouseEvent->button() == Qt::LeftButton)
                {
                    // 发射信号到上层, 跳转到哪个类型
                    emit labelClicked(strList.at(i));
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    return QObject::eventFilter(obj, event);
}

void IndexPage::slotBlogInfo(QMap<QString, QString> map, int type)
{
    // 发射信号到上层，跳转到某个博客，或者某个作者
    emit signalBlogInfo(map, type);
}

void IndexPage::slotDownloadRes(QMap<QString, QString> map)
{
    emit signalDownloadRes(map);
}
void IndexPage::slotUserRes(QString user)
{
    emit signalUserRes(user);
}
IndexPage::~IndexPage()
{
    delete ui;
}
